﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute.ModelValidate
{
    [AttributeUsage(AttributeTargets.Property)]
    public class CustomLengthAttribute : AbstractValidateAttribute
    {
        /// <summary>
        /// 数据的最短
        /// </summary>
        private int _MinLength; //

        /// <summary>
        /// 数据最长
        /// </summary>
        private int _MaxLength;//  

        //public string ErrorMessage = "验证失败";

        public CustomLengthAttribute(int minLength, int maxLength)
        {
            _MinLength = minLength;
            _MaxLength = maxLength;
        }

        public override bool Validate(object oValue)
        {
            if (oValue == null)
            {
                return false;
            }
            else if (oValue.ToString().Length < _MinLength)
            {
                return false;
            }
            else if (oValue.ToString().Length > _MaxLength)
            {
                return false;
            }
            return true;
        }
    }
}
